package mx.com.mindbits.bandpay.utils;


import java.net.ConnectException;
import java.net.SocketException;

import javax.mail.internet.InternetAddress;

import android.content.Context;
import android.os.Environment;

public class SendMail {
	private static  Mail mail;
	private static String[] adressesArray;
	private static boolean send = false;
	private static StringBuilder mailBody;

	
	public static boolean mailSend(String adresses, String longitud, String latitud,
			Context con, InternetAddress[] _address, String adicional, String subject ) throws Exception, 
	ConnectException, SocketException{
		
		mailBody = new StringBuilder();
		String path = Environment.getExternalStorageDirectory().toString();
		path += "/ticket.jpg";
		mail = new Mail(con, _address);
		adressesArray = adresses.split("\\,");
		mailBody.append("<!doctype html>"); 
		mailBody.append("<html dir=\"ltr\" lang=\"en\">");
		mailBody.append("		<head>"); 
		mailBody.append("		<meta charset=\"utf-8\" />"); 
		mailBody.append("		<meta name=\"viewport\" content=\"user-scalable=yes, width=device-width\" />");
		mailBody.append("		<title>Responsive table hiding children example</title>"); 
		mailBody.append("		<style>");
		mailBody.append("			#iseqchart	{");
		mailBody.append("			border:1px solid #000");
		mailBody.append("			border-collapse:collapse");
		mailBody.append("			font-family:Arial, Sans-Serif");
		mailBody.append("			font-size:12px");
		mailBody.append("			text-align:right");
		mailBody.append("			}");			
		mailBody.append("		#iseqchart th	{");
		mailBody.append("			border:1px solid #333");
		mailBody.append("			padding:3px 6px");
		mailBody.append("		}");
		mailBody.append("		#iseqchart td	{");
		mailBody.append("			border:1px solid #999");
		mailBody.append("			padding:3px 6px	");
		mailBody.append("			}");
		mailBody.append("		.neg	{");
		mailBody.append("			color:red");
		mailBody.append("		}");
		mailBody.append("		.pos	{");
		mailBody.append("			color:green");
		mailBody.append("		}");
		mailBody.append("		.bg{");
		mailBody.append("			background-color:#0101DF;");
		mailBody.append("			color:#FFF;");
		mailBody.append("		 }");
		mailBody.append("		 .lb{");
		mailBody.append("			 color:#F60;");
		mailBody.append("		}");
		mailBody.append("		.bgpar{");
		mailBody.append("			background-color:#FFF;");
		mailBody.append("			color:#E05206;");
		mailBody.append("			}");
		mailBody.append("		.bgnon{");
		mailBody.append("			background-color:#666;");
		mailBody.append("			color:#FFF;");
		mailBody.append("			}");	
		mailBody.append(" 		#map_canvas {");
        mailBody.append("				width: 500px;");
        mailBody.append("				height: 300px;");
        mailBody.append("			}");
		mailBody.append("		@media only screen and (max-width: 768px) {");
		mailBody.append("			#turnover, tr td:nth-child(9)		{ display:none visibility:hidden }");
		mailBody.append("		}");
		mailBody.append("		@media only screen and (max-width: 420px) {");
		mailBody.append("			#changepercent, tr td:nth-child(4)	{ display:none visibility:hidden }");
		mailBody.append("			#yhigh, tr td:nth-child(5)			{ display:none visibility:hidden }");
		mailBody.append("			#ylow, tr td:nth-child(6)			{ display:none visibility:hidden }");
		mailBody.append("			#turnover, tr td:nth-child(9)		{ display:none visibility:hidden }");
		mailBody.append("		}");
		mailBody.append("		@media only screen and (max-width: 320px) {");
		mailBody.append("			#changepercent, tr td:nth-child(4)	{ display:none visibility:hidden }");
		mailBody.append("			#yhigh, tr td:nth-child(5)			{ display:none visibility:hidden }");
		mailBody.append("			#ylow, tr td:nth-child(6)			{ display:none visibility:hidden }");
		mailBody.append("			#dhigh, tr td:nth-child(7)			{ display:none visibility:hidden }");
		mailBody.append("			#dlow, tr td:nth-child(8)			{ display:none visibility:hidden }");
		mailBody.append("			#turnover, tr td:nth-child(9)		{ display:none visibility:hidden }");
		mailBody.append("		}");
		mailBody.append("		</style>");		
		mailBody.append("			</head>"); 			
		mailBody.append("		<body>");
		mailBody.append("		<table id=\"iseqchart\" width=\"100%\">");
		mailBody.append("  <tr>");
		mailBody.append("    <td class=\"bg\">B&PAY</td>");
		mailBody.append("  </tr>");
		mailBody.append("  <tr>");
		mailBody.append("				    <td>Este mail adjunta la imagen de su ticket. Asi como la ubicacion geografica del lugar donde realizo la transaccion.</td>  </tr>");
		mailBody.append("  <tr>              <td> " +  adicional + " </td> ");
		mailBody.append("				  </tr>");
		mailBody.append("<tr>");
		mailBody.append("<td>Ubicacion (pulsa para agrandar)</br><div>");
		mailBody.append("<a href=\"http://maps.google.com/maps?q="+latitud+","+longitud+"&ll="+latitud+","+longitud+"&z=17\"><img src=\"http://maps.google.com/maps/api/staticmap?center="+latitud+","+longitud+"&zoom=13&size=125x75&sensor=true\"></a></div></td>");
		mailBody.append("</tr>");
		mailBody.append("				  <tr>");
		mailBody.append("				    <td><hr>");
		mailBody.append("				    <table id=\"iseqchart \" width=\"100%\">");
		mailBody.append("				        	<tr>");
		mailBody.append("								<td class=\"bg\">Aviso de privacidad: <a href =\""+ mail.get_privacity()  +"\"> Aviso de privacidad</a> " +
				"</br>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, </td>");
		mailBody.append("				          </tr>");                     
		mailBody.append("				      </table>");
		mailBody.append("				    </td>");
		mailBody.append("				  </tr>");
		mailBody.append("				</table>");
		mailBody.append("						</br>");
		mailBody.append("				</body>");
		mailBody.append("				</html>");
		
				
		
		
		mail.setTo(adressesArray);
		mail.setSubject(subject);
		mail.setBody(mailBody.toString());
		mail.addAttachment(path);			
		if(mail.send()){
				send = true;
		}else{
				send = false;
		}
								
		return send;
	} 
	
}
